<style>

</style>
<div class="layui-layout order-wrapper" style="padding:0 20px;">
    <div id="search-wrapper">
        <div class="layui-form">
            <div class="layui-row">
                <div class="layui-inline">
                    {{if eq .isSend 1}}
                        <button class="layui-btn layui-btn-disabled add" disabled>新增</button>
                    {{else}}
                        <button class="layui-btn layui-btn-normal add">新增</button>
                    {{end}}
                </div>
            </div>
        </div>
    </div>
    <table class="layui-hide" id="table_list" lay-filter="table_filter">
    </table>
</div>
<script>
    layui.use(['table','form'], function(){
        var table = layui.table;
        var $ = layui.$;
        var isSend = '{{.isSend}}';
        //方法级渲染
        table.render({
            elem: '#table_list'
            ,url: '/group/itemtable?taskId=' + {{.taskId}} + '&appName=' + {{.appName}}
            ,cols: [[
                {field:'Sort', title: '序号', align:'center',width:''},
                {field:'AppName', title: '应用名称', align:'center',width:''},
                {field:'Title', title: '标题', align:'center',width:''},
                {field:'Data', title: '链接', align:'center',width:''},
                {field:'', title: '操作', align:'center',width:'',templet:function (d) {
                        if (isSend == 1){
                            return  `<button class="layui-btn layui-btn-xs layui-btn-disabled" disabled>编辑</button>
                                    <button class="layui-btn layui-btn-disabled layui-btn-xs" disabled>删除</button>`
                        }else{
                            return  `<button class="layui-btn layui-btn-xs" onclick="editItem(`+d.Id+`,'`+d.AppName+`','`+d.Title+`','`+d.Data+`')">编辑</button>
                                    <button class="layui-btn layui-btn-danger layui-btn-xs" onclick="delItem(`+d.Id+`)">删除</button>`
                        }
                    }},
            ]]
            ,id: 'listReload'
            ,page: {
                curr: location.hash.replace('#!fenye=', '') //获取起始页
                ,hash: 'fenye' //自定义hash值
            }
            ,height: "full-200"
        });

        var active = {
            reload: function(){
                table.reload('listReload', {
                    where:{
                        appId:$('.app').val()
                    },
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }
        };


        $('#search-wrapper .search').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';

        });

        // $(".export").click(function(){
        //     var urlParam = "?page=1&limit=99999&startTime="+$('#start_time').val() + '&endTime=' + $('#end_time').val() + "&export=1";
        //     window.open('/operate/table'+urlParam)
        // });

        $(".add").click(function(){
            var appName = {{.appName}};
            var taskId = {{.taskId}};
            var isNone = $(".layui-none").length;
            layer.open({
                area: ['500px', 'auto'],
                content: `
                    <form class="layui-form">
                      <div class="layui-form-item">
                        <label class="layui-form-label">公众号：</label>
                        <div class="layui-input-block" style="line-height: 36px;text-align: center">
                            ` + appName + `
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">书本Id：</label>
                        <div class="layui-input-block">
                          <input type="text" class="layui-input bookId" placeholder="请输入书本id" onblur="getBookById()">
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">书名：</label>
                        <div class="layui-input-block">
                            <div class="bookName" style="height: 36px;line-height: 36px"></div>
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">标题：</label>
                        <div class="layui-input-block">
                             <textarea name="tip" placeholder="请输入标题" class="layui-textarea tip"></textarea>
                        </div>
                      </div>
                       <div class="layui-form-item">
                        <label class="layui-form-label">链接：</label>
                        <div class="layui-input-block" id="isOuter">
                           <textarea name="data" placeholder="请输入链接" class="layui-textarea data"></textarea>
                        </div>
                      </div>
                    </form>`,
                yes: function(index, layero){
                    //do something
                    var tip = $('.tip').val();
                    var data = $(".data").val();

                    if(tip == '' || data == ''){
                        layer.msg("请将信息填写完整");
                        return
                    }
                    $.ajax({
                        type:'post',
                        url:'{{urlfor "GroupController.AddItem"}}',
                        data:{taskId:taskId,tip:tip,data:data},
                        success:function (data) {
                            if(data.status == 0){
                                if(isNone == 1){
                                    active.reload();
                                }else{
                                    $(".layui-laypage-btn").click();
                                }
                                layer.msg(data.message);
                            }else{
                                layer.msg(data.message);
                            }
                        }
                    })
                    //layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        });

    });

    function editItem(id,appName,title,data) {
        layer.open({
            area: ['500px', 'auto'],
            content: `
                    <form class="layui-form">
                      <div class="layui-form-item">
                        <label class="layui-form-label">公众号：</label>
                        <div class="layui-input-block" style="line-height: 36px;text-align: center">
                            ` + appName + `
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">书本Id：</label>
                        <div class="layui-input-block">
                          <input type="text" class="layui-input bookId" placeholder="请输入书本id" onblur="getBookById()">
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">书名：</label>
                        <div class="layui-input-block">
                            <div class="bookName" style="height: 36px;line-height: 36px"></div>
                        </div>
                      </div>
                      <div class="layui-form-item">
                        <label class="layui-form-label">标题：</label>
                        <div class="layui-input-block">
                             <textarea name="tip" placeholder="请输入标题" class="layui-textarea tip">` + title + `</textarea>
                        </div>
                      </div>
                       <div class="layui-form-item">
                        <label class="layui-form-label">链接：</label>
                        <div class="layui-input-block" id="isOuter">
                           <textarea name="data" placeholder="请输入链接" class="layui-textarea data">` + data + `</textarea>
                        </div>
                      </div>
                    </form>`,
            yes: function(index, layero){
                //do something
                var tip = $('.tip').val();
                var data = $(".data").val();

                if(tip == '' || data == ''){
                    layer.msg("请将信息填写完整");
                    return
                }
                $.ajax({
                    type:'post',
                    url:'{{urlfor "GroupController.EditItem"}}',
                    data:{id:id,tip:tip,data:data},
                    success:function (data) {
                        if(data.status == 0){
                            $(".layui-laypage-btn").click();
                            layer.msg(data.message);
                        }else{
                            layer.msg(data.message);
                        }
                    }
                })
                //layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    }

    function delItem(id) {
        layer.open({
            title:'提示',
            content: '确定要删除吗？',
            yes: function(index, layero){
                //do something
                $.ajax({
                    type:'post',
                    url:'{{urlfor "GroupController.DelItem"}}',
                    data:{id:id},
                    success:function (data) {
                        if(data.status == 0){
                            $(".layui-laypage-btn").click();
                            layer.msg(data.message);
                        }else {
                            layer.msg(data.message);
                        }
                    }
                });
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    }

    function getBookById() {
        $.ajax({
            type:'get',
            url:'/book/getbook?id='+$(".bookId").val(),
            success:function (data) {
                if(data.code == 0 && data.data){
                    $(".bookName").html(data.data.Name);
                    $(".tip").html(data.data.Tip);
                    $(".data").html("https://yhsy.98nice.com/view/home/chapter/" + data.data.FirstSerialId + "/yhsy");
                }else {
                    $(".bookName").html("查询不到该小说").attr("data-id",0);
                }
            }
        })
    }

</script>